/**
 * Creation Date: 	20100401
 * Description: 	controllerCustomerSatisfactionSurvey manages CustomerSatisfactionSurveyEdit
 *					
 *				
 * Author: 	ABSI - Bob Ceuppens
 */

public without sharing class controllerCustomerSatisfactionSurvey {
	
	/**
	*	Logic:
	*	Edit on Customer_Satisfaction_Survey__c is overwritten to CustomerSatisfactionSurveyEdit
	* 	CustomerSatisfactionSurveyEdit calls to redirectToCustomerSatisfactionSurvey
	*	redirectToCustomerSatisfactionSurvey gets the values of the survey of the previous month and prefills the standard edit page
	*
	*/
	
	private final ApexPages.StandardController controller;
	private Customer_Satisfaction_Survey__c currentSurvey  {get;set;}

	// Helper Map to hold information about the Survey
	public Map<String, String> dataMap = new Map<String, String>();
	
	public controllerCustomerSatisfactionSurvey(ApexPages.StandardController stdController) {
		this.controller = stdController;
		this.init();
	}
	
	public void Init() {
		currentSurvey = (Customer_Satisfaction_Survey__c) controller.getRecord();
		if (currentSurvey.Id == null) {
			return;
		}
		// Need to do an extra SOQL to get all values 
		currentSurvey = [Select c.VCMresponse_11__c, /*c.VCMresponse_10__c,*/ c.VCMresponseTotal__c, c.VCMresponse9__c, c.VCMresponse8__c, c.VCMresponse7__c, c.VCMresponse6__c, c.VCMresponse5__c, c.VCMresponse4__c, c.VCMresponse3__c, c.VCMresponse2__c, c.VCMresponse1__c, c.VCMResponse_13__c, c.VCMResponse_12__c, c.SystemModstamp, c.Survey_Year__c, c.Survey_Month__c, c.RecordTypeId, c.Name, c.Manager_Email__c, c.LastModifiedDate, c.LastModifiedById, c.IsDeleted, c.Id, c.CustomerResponseTotal__c, c.CustomerResponse1__c, c.CustomerResponse13__c, c.CustomerResponse12__c, c.CustomerResponse11__c, c.CurrencyIsoCode, c.CreatedDate, c.CreatedById, c.Contact__c
							From Customer_Satisfaction_Survey__c c
							Where c.Id =: currentSurvey.Id];
		System.debug('################## ' + 'currentSurvey= ' + currentSurvey + '################');		
	}
	
 	public PageReference redirectToCustomerSatisfactionSurvey(){
 		
		PageReference pageRef;

		String PrefilURL = '';
		
		//Only prefill if the Surevey is not yet filled in
		if (currentSurvey.VCMresponse1__c == null) {
			PrefilURL = this.getPrefilURL();
		}

		String URL = '/' + currentSurvey.Id + '/e?';
		URL +=  PrefilURL;
		URL += '&retURL=%2F'+ currentSurvey.Id + '&sfdc.override=0&nooverride=1';
		
		pageRef = new PageReference(URL);
		pageRef.setRedirect(true);
		
		System.debug('################## ' + 'PageReference= ' + pageRef + '################');
		
		return pageRef;
 	}
	
	/**
	*	
	*	Method builds up an URL 
	*
	*/
	 	
	
	public String getPrefilURL () {
	 	
	 	Map<String, String> dataMap = new Map<String, String>();
	 	
	 	Integer currentMonthInt = Integer.valueOf(currentSurvey.Survey_Month__c);
	 	Integer currentYearInt = Integer.valueOf(currentSurvey.Survey_Year__c);
	 	
	 	String previousMonth = String.valueOf(currentMonthInt -1);
		String previousYear = String.valueOf(currentYearInt);
	 	
	 	//If january, we need to go back to december of the previous year
	 	if (currentMonthInt==1) {
	 		previousMonth = '12';
	 		previousYear = String.valueOf(currentYearInt-1);
	 	}
	 	
	 	//Month needs to be in format 'XX'
		if (previousMonth.length() == 1) {
			previousMonth = '0' + previousMonth;
		}
		
		System.debug('################## ' + 'previousMonth= ' + previousMonth + '  previousYear= ' + previousYear + '################');
		
		// Dynamicall create a SOQL to get all filednames
		// Simulates [Select *]
		Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.Customer_Satisfaction_Survey__c.fields.getMap();
		Set<String> fieldSet = fieldMap.keySet();
		System.debug('################## ' + 'fieldSet= ' + fieldSet + '################');
		
		String SOQL =  'Select ';
		for (String fieldName : fieldSet){
	    	System.debug('fieldName: ' + fieldName);
	     	SOQL += fieldName + ',';  
	  	} 
	  	SOQL = SOQL.substring(0,SOQL.length()-1);  //Remove the last , unnecessary comma
	  	
		SOQL += ' From Customer_Satisfaction_Survey__c c';
		SOQL += ' Where c.Contact__c = \'' +  currentSurvey.Contact__c + '\'';
		SOQL += ' And c.Survey_Month__c = \'' +  previousMonth + '\'';
		SOQL += ' And c.Survey_Year__c = \'' +  previousYear + '\'';
		
		System.debug('################## ' + 'SOQL= ' + SOQL + '################');
		
		List<sObject> S = Database.query(SOQL);
		Customer_Satisfaction_Survey__c[] previousSurveyList = (List<Customer_Satisfaction_Survey__c>) S;
		
		System.debug('################## ' + 'previousSurveyList.size= ' + previousSurveyList.size() + '################');
		
		//If there are no or multiple survey's, the logic is unclear and we step out
		if (previousSurveyList.size() != 1) {
			return '';
		}

		//Get the first - and only value from the Array and build up a Hashmap of values for ease of future access
		Customer_Satisfaction_Survey__c previousSurvey = previousSurveyList.get(0);
		for (String fieldName:fieldSet) {
			dataMap.put(fieldName, String.valueOf(previousSurvey.get(fieldName))) ;
		}

		System.debug('################## ' + 'dataMap= ' + dataMap + '################');
		
		//Gets the Array of Id's of the fields that need to be prefilled
		//these Id's point to the fields on the standard pages
		//if you add these Id's and a value to the URL, the field will be prefilled on the standard edit page 
		Map<String, String> fieldsToBePrefilled = FinalConstants.getSurveyFields();
		
		String URL='';
		
		for (String fieldName : fieldsToBePrefilled.keySet()) {
			URL += fieldsToBePrefilled.get(fieldName) + '=' + dataMap.get(fieldName.toLowerCase()) + '&';
		}
	  	URL = URL.substring(0,URL.length()-1);  //Remove the last , unnecessary &
		
		System.debug('################## ' + 'URL= ' + URL + '################');
		
		return URL;
		
	}

}